package com.example.ecommerce.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ecommerce.entity.Product;
import com.example.ecommerce.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductMapper productMapper;

    public List<Product> getProducts(int page, int size, String search) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (search != null && !search.trim().isEmpty()) {
            queryWrapper.like("name", search);
        }
        // 分页计算
        int offset = (page - 1) * size;
        return productMapper.selectList(queryWrapper.last("LIMIT " + offset + ", " + size));
    }

    public List<Product> getProductsByUid(Long uid) {
        return productMapper.selectByUid(uid);
    }

    public boolean updateProduct(Product product) {
        return productMapper.updateById(product) == 1;
    }

    public boolean deleteProductById(Long productId) {
        return productMapper.deleteById(productId) == 1;
    }

    public boolean addProduct(Product product){
        productMapper.insert(product);
        return true;
    }

    public int deleteByMerchantId(Long merchantId) {
        QueryWrapper<Product> query = new QueryWrapper<>();
        query.eq("uid", merchantId);
        return productMapper.delete(query);
    }
}


